package gu.sql2java.json;

import static com.google.common.base.MoreObjects.firstNonNull;

import com.alibaba.fastjson.serializer.SerializeConfig;
import com.gitee.l0km.beanfilter.core.json.FastjsonFilterableSerializer;

import gu.sql2java.BaseBean;
import gu.sql2java.RowMetaData;

/**
 * @author guyadong
 * @since 3.32.0
 */
public class FastjsonSerializer extends FastjsonFilterableSerializer{
	public FastjsonSerializer(Class<? extends BaseBean> beanType) {
		super(beanType);
	}
	/**
	 * define FASTJSON serializer for all class derived from BaseBean using {@link SerializeConfig#getGlobalInstance()} 
	 */
	public static void install() {
		install(SerializeConfig.getGlobalInstance());
	}
	/**
	 * define FASTJSON serializer for all class derived from BaseBean  
	 * @param config using {@link SerializeConfig#getGlobalInstance()} instead of {@code null} 
	 */
	public static void install(SerializeConfig config) {
		config = firstNonNull(config,SerializeConfig.getGlobalInstance());
		for(RowMetaData metaData:RowMetaData.allMetaDataList()){
			config.put(
					metaData.beanType,
					new FastjsonSerializer(metaData.beanType));
		}
	}
}
